home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1990: Discy Business / Discy Business.2mg / DEV.CD / TOOLS / SAMPLES / HP / HP.ASM / WINDOW.ASM < prev   
Encoding:
Assembly Source File  |  1989-03-24  |  31.8 KB  |  1,075 lines  |  [B0] Apple IIgs Source Code (0x0003)

  1.  
  2. ******************************************************************************
  3. *                                                                            *
  4. *         HodgePodge:  An example Apple IIGS Desktop application             *
  5. *                                                                            *
  6. *         Written in 65816 Assembler by the Apple IIGS Tools Team            *
  7. *  Modified by Ben Koning for "Programmer's Introduction to the Apple IIGS"  *
  8. *                                                                            *
  9. *             Copyright (c) 1986-87 by Apple Computer, Inc.                  *
  10. *                                                                            *
  11. *   ----------------------------------------------------------------------   *
  12. *                                                                            *
  13. *           ASM65816 Code file "WINDOW.ASM" -- Open/Close windows            *
  14. *                                                                            *
  15. ******************************************************************************
  16.  
  17.  
  18.  
  19. ****************************************************************
  20. *
  21. * HideAllWindows
  22. *
  23. ****************************************************************
  24.  
  25. HideAllWindows START
  26.                using GlobalDATA
  27.  
  28.                stz VIndex               ;index for list of what was vis.
  29.  
  30. HideLoop       PushLong #0              ;hide 'em all, looks neater
  31.                _FrontWindow
  32.                ldx VIndex
  33.                pla
  34.                sta VTable,x
  35.                pla
  36.                sta VTable+2,x
  37.                cmp #0
  38.                bne dohid
  39.                lda Vtable,x
  40.                bne dohid
  41.                rts                      ;all vis. windows hidden now
  42.  
  43. doHid          pha
  44.                lda Vtable,x
  45.                pha
  46.                _HideWindow
  47.                Lda Vindex
  48.                clc
  49.                adc #4
  50.                sta Vindex
  51.                bra HideLoop
  52.  
  53.                END
  54.  
  55.  
  56.  
  57. ****************************************************************
  58. *
  59. * DoOpenItem :
  60. *
  61. *   1) Make sure not too many windows open already -- may show dialog
  62. *
  63. *   2) Call AddToMenu to add its name into the "windows" menu list
  64. *
  65. *
  66. ****************************************************************
  67.  
  68. DoOpenItem     START
  69.  
  70.                using GlobalDATA
  71.                using FontDATA
  72.  
  73.                lda Windex               ;Check if too many windows open already
  74.                cmp #LastWind            ;... otherwise "window" menu overflows!
  75.                bcc OkToOpen             ;No, so go ahead and try to open one
  76.                jsr ManyWindDialog       ;Yes, so confront user with dialog box
  77.                sec                      ;Set carry because it didn't happen
  78. Done           rts
  79.  
  80. OkToOpen       jsr OpenWindow
  81.                bcs Done                 ;if we didn't open, don't add it
  82.                jmp AddToMenu            ;Add it to the menu list and exit
  83.  
  84.                END
  85.  
  86.  
  87.  
  88. ****************************************************************
  89. *
  90. * DoSaveitem :
  91. *
  92. *
  93. ****************************************************************
  94.  
  95. DoSaveItem     START
  96.                using GlobalDATA
  97.                using IOData
  98.  
  99.  
  100.                pushlong #0              ;it's the front window we're saving
  101.                _FrontWindow
  102.                pla
  103.                sta whichwindow
  104.                plx
  105.                stx whichwindow+2          ;get result for pushing in a sec.
  106.  
  107.                PUSHLONG #0              ;space for result
  108.                PUSHLONG whichwindow
  109.                _GetWrefCon              ;refcon has handle to data
  110.  
  111.                pla
  112.                plx
  113.  
  114.                jsr deref
  115.  
  116.                sta 0
  117.                sta Refptr
  118.                stx 2
  119.                stx Refptr+2
  120.  
  121.                ldy #oFlag               ;check if picture
  122.                lda [0],y
  123.                beq oktosav              ;save only type 0 windows
  124.                rts
  125.  
  126. oktosav        PUSHLONG #0              ;space for result
  127.                PUSHLONG whichwindow
  128.                _GetWTitle
  129.  
  130.                pla
  131.                sta NamePtr
  132.                plx
  133.                stx NamePtr+2
  134.  
  135.                PushWord #20             ; x loc
  136.                PushWord #20             ; y loc
  137.                PushLong #Prompt2        ; prompt string pointer
  138.                PushLong NamePtr         ; File name
  139.                Pushword #15             ; Max file name length
  140.                PushLong #reply          ; reply list result
  141.                _SFPutFile
  142.  
  143.                lda r_good               ; <> 0 means OK to load it
  144.                bne Saveitoff
  145.  
  146.                rts
  147.  
  148.  
  149. Saveitoff      anop
  150.  
  151.                _WaitCursor
  152.  
  153.                lda Refptr
  154.                sta 0
  155.                lda Refptr+2
  156.                sta 2
  157.  
  158.                ldy #oHandle
  159.                lda [0],y
  160.                sta PicHandle
  161.                iny
  162.                iny
  163.                lda [0],y
  164.                sta PicHandle+2          ; this de-refd, is the data to write oute pichandle (we'll de-allocate)
  165.  
  166.                lda PicHandle
  167.                ldx PicHandle+2
  168.                jsr DeRef
  169.                sta PicDestOUT
  170.                stx PicDestOUT+2         ; now pointing to what we write
  171.  
  172.                lda #R_fullPN            ; put pointer to name in i/o param block
  173.                sta NamePtr
  174.                lda #^R_FullPN
  175.                sta NamePtr+2
  176.  
  177.  
  178. nopack         Jsr SaveOne
  179.                Bcs OuttaHere
  180.  
  181. fixnm          lda refptr               ; now fix up name
  182.                clc
  183.                adc #oLength             ; where the name will go
  184.                sta 0                    ; save in 0,2 also for later indirect
  185.                sta refptr
  186.                lda refptr+2
  187.                adc #0
  188.                sta 2
  189.                sta refptr+2
  190.  
  191.                lda r_Fname
  192.                and #$00FF
  193.                tay
  194.                sep #%00100000
  195.                longa off
  196. cpynm          lda r_Fname,y
  197.                sta [0],y
  198.                dey
  199.                bpl cpynm
  200.                rep #%00100000
  201.                longa on
  202.  
  203.                pushlong refptr          ;(it points to string now, remember?)
  204.                pushlong whichwindow
  205.                _SetWTitle
  206.  
  207.                lda #0                   ; re-calc size
  208.                pha
  209.                pha
  210.                PushWord #WindowsMenuId
  211.                _CalcMenuSize
  212.  
  213. OuttaHere      lda PicHandle
  214.                ldx PicHandle+2
  215.                jsr Unlock
  216.  
  217.                _InitCursor
  218.  
  219.                rts
  220. refptr         ds   4
  221.                END
  222.  
  223.  
  224.  
  225. ****************************************************************
  226. *
  227. * OpenWindow:
  228. *
  229. *   1) Call SFGETFILE to get name of file to display in window
  230. *      (or the dialog to select font if Display Font call)
  231. *
  232. *   2) Gets memory for, and loads the picture/font data into memory
  233. *
  234. *   3) Allocates a new window
  235. *          a) puts handle to MyWindowInfo in WrefCon
  236. *          b) note that routine to draw picture contents is set to "PAINT"
  237. *          c) note for font draw contents is "DISPFONTWINDOW"
  238. *
  239. *   The definition of MyWindowInfo is contained in global data
  240. *
  241. * If the menu manager is being used to add itemlist items with the file
  242. * name, it will squeeze the \N etc. together (see AddToMenu).  In any
  243. * case, the file name string for the window title can still be found
  244. * starting at this area+5
  245. *
  246. * returns:  carry set - didn't open it (user cancelled SFGETFILE)
  247. *           carry clear - window opened
  248. *
  249. ****************************************************************
  250. OpenWindow     START
  251.                using GlobalDATA
  252.                using IOData
  253.                using FontDATA
  254.                using WindowData
  255.  
  256.                lda TaskData
  257.                cmp #ShowFontID          ; is it open for font window?
  258.                bne AskUser
  259.                jsr DoChooseFont
  260.                bcs stp
  261.                jmp DoTheOpen
  262. stp            rts                      ;cancelled choose font
  263.  
  264.  
  265.  
  266. ********
  267. *
  268. *  call SFGETFILE to request the file name
  269. *
  270. ********
  271. AskUser        lda #20
  272.                pha                      ; x loc.
  273.                lda #20
  274.                pha                      ; y loc.
  275.                PushLong #Prompt         ; prompt string pointer
  276.                PushLong #OpenFilter     ; Do dimmed display of unloadables
  277.                PushLong #0              ; list of types to include -- 0 for all
  278.                PushLong #reply          ; reply list result
  279.                _SFGetFile
  280.                lda r_good               ; <>0 means OK to load it
  281.                bne loaditup
  282.                sec                      ; carry set return: didn't open
  283. HandleError    rts
  284.  
  285. *********
  286. *
  287. * Get space for the picture file
  288. *
  289. ********
  290.  
  291. LoadItUp       anop
  292.  
  293.                _WaitCursor
  294.  
  295.                PushLong #0              ; space
  296.                PushLong #$8000          ; size
  297.                PushWord MyID            ; id
  298.                PushWord #$0000          ; no restrictions
  299.                PushLong #0              ; loc not important
  300.                _NewHandle
  301.  
  302.                pla
  303.                sta PicHandle
  304.                plx
  305.                stx PicHandle+2
  306.  
  307.                bcs HandleError          ; if error occured from no handle
  308.  
  309.                jsr Deref                ; derefence handle (in a,x)
  310.  
  311.                sta PicDestIN            ; put pointer in i/o param block
  312.                stx PicDestIN+2
  313.  
  314. DoTheOpen      PushLong #0              ; space
  315.                PushLong #MyWinfoSize    ; size
  316.                PushWord MyID            ; id
  317.                PushWord #$C000          ; fixed and locked
  318.                PushLong #0              ; loc not important
  319.                _NewHandle
  320.  
  321.                pla
  322.                sta refcon
  323.                plx
  324.                stx refcon+2
  325.  
  326.                bcs HandleError
  327.  
  328.                jsr deref                ;de ref. for storing stuff into
  329.  
  330.                sta refptr
  331.                sta 0
  332.                stx refptr+2
  333.                stx 2
  334.  
  335. *********
  336. *
  337. * Start by assuming this will be a picture window (not a font window).
  338. * We set the address of the drawing routine to PAINT and set the flag
  339. * in MyWindowInfo record to 0 indicating picture.
  340. *
  341. *********
  342.  
  343.          lda #Paint                     ; first the address of the Paint
  344.          sta DrawRtn                    ; routine
  345.          lda #^Paint
  346.          sta DrawRtn+2
  347.          ldy #oFlag                     ; Now set the flag field
  348.          lda #0
  349.          sta [0],y
  350.          
  351.  
  352. ;------------------------------------------------------------
  353. ;
  354. ; Now we see if that silly assumption above was correct.
  355. ;
  356.          lda TaskData                   ; look at the menu item that
  357.          cmp #ShowFontID                ; brought us here.
  358.          bne setIO                      ; not the font one so go on
  359.  
  360.          lda #1                         ; fix the flag field
  361.          ora MonoFlag                   ; set bit 1 if monospaced font
  362.          sta [0],y                      ; (y still set)
  363.          lda DesiredFont                ; put the chosen fontid where
  364.          sta PicHandle                  ; we will later put it in
  365.          lda DesiredFont+2              ; the MyWindowInfo record
  366.          sta PicHandle+2
  367.  
  368.          lda #DispFontWindow            ; finally, fix the pointer to the
  369.          sta DrawRtn                    ; drawing routine
  370.          lda #^DispFontWindow
  371.          sta DrawRtn+2
  372.          jmp DoMovNam
  373.                
  374. SetIO    lda #R_fullPN                  ; put pointer to name in i/o param block
  375.          sta NamePtr
  376.          lda #^R_FullPN
  377.          sta NamePtr+2
  378.  
  379. *********
  380. *
  381. * load picture in "NamePtr" into "PicDest"
  382. *
  383. *********
  384. *  check for packed or unpacked file load
  385.  
  386.  
  387.                jsr LoadOne              ; load it
  388.                bcc DoMovNam
  389.  
  390. IOError        anop                     ; There was an error loading the file
  391.                PushLong RefCon          ; so dispose of the memory that we
  392.                _DisposeHandle           ; allocated while trying to create
  393.                PushLong PicHandle       ; this window
  394.                _DisposeHandle
  395.                sec
  396.                rts
  397.  
  398.  
  399. **********
  400. *
  401. * Move the files name into the param block
  402. *
  403. **********
  404.  
  405. DoMovNam       lda refptr               ;use zero page for indirect stores
  406.                sta 0
  407.                lda refptr+2
  408.                sta 2
  409.  
  410.                lda pichandle            ;into the recfon area (refptr)
  411.                ldy #oHandle
  412.                sta [0],y
  413.                iny
  414.                iny
  415.                lda pichandle+2
  416.                sta [0],y
  417.  
  418.                ldy #oBlank              ; Put blank in record at blank field
  419.                lda #'  '                ; (note this 16 bit store will over-
  420.                sta [0],y                ; write the length field but we don't
  421. ;                                       ; care since we fix it below.
  422.  
  423.                lda refptr
  424.                clc
  425.                adc #oLength             ; where the name will go
  426.                sta windaddr
  427.                sta 0                    ; save in 0,2 also for later indirect
  428.                lda refptr+2
  429.                adc #0
  430.                sta windaddr+2
  431.                sta 2
  432.  
  433.                lda r_Fname
  434.                and #$00FF
  435.                cmp #MaxNameSize
  436.                bmi NameLenOK
  437.                lda #MaxNameSize
  438.                sep #%00100000
  439.                sta r_Fname
  440.                rep #%00100000
  441. NameLenOK      tay
  442.                sep #%00100000
  443.                longa off
  444. cpynm          lda r_Fname,y
  445.                sta [0],y
  446.                dey
  447.                bpl cpynm
  448.                rep #%00100000
  449.                longa on
  450.  
  451.                ldy #350                 ;adjust max siz
  452.                ldx #640                 ;adjust pixel count
  453.                stx DataWidth
  454.                stx mcw
  455.                sty IsizPos+6
  456.  
  457. ;------------------------------------------------------------
  458. ;
  459. ; Set up the DataHeight based on the type of window it is.
  460. ;
  461.                lda #200                 ; assume picture and make 200 the max
  462.                sta DataHeight           ; height
  463.                lda RefPtr               ; now see what it really is
  464.                sta 0
  465.                lda RefPtr+2
  466.                sta 2
  467.  
  468.                lda TaskData
  469.                cmp #OpenWID
  470.                beq IsPicture
  471.  
  472.                PushLong #0              ; save this on the stack
  473.                _GetFontID
  474.  
  475.                ldy #oFontID+2           ; now install the font that will
  476.                lda [0],y                ; be used in the current port
  477.                pha
  478.                dey
  479.                dey
  480.                lda [0],y
  481.                pha
  482.                PushWord #0
  483.                _InstallFont
  484.  
  485.                PushLong #FIRecord       ; get the font info so can get
  486.                _GetFontInfo             ; ascent and descent.
  487.  
  488.                PushLong #0              ; space for result
  489.                lda ascent               ; now multiply sum of ascent &
  490.                clc                      ; descent by num lines to draw
  491.                adc Descent
  492.                pha
  493.                PushWord #NumLines+1
  494.                _Multiply
  495.                pla                      ; put result in DataHeight
  496.                sta DataHeight
  497.                pla                      ; strip off high word of nothing
  498.  
  499.                jsr FindMaxWidth
  500.  
  501.                PushWord #0              ; using saved fontid on stack
  502.                _InstallFont             ; re-install the orig font
  503.  
  504. IsPicture      anop
  505. *********
  506. *
  507. *  offset upperleft corner for opening of window
  508. *
  509. *********
  510.                ldx #0
  511. MovOff         lda ISizPos,x
  512.                clc
  513.                adc Wyoffset
  514.                sta SizPos,x
  515.                lda ISizPos+2,x
  516.                clc
  517.                adc Wxoffset
  518.                sta SizPos+2,x
  519.                inx
  520.                inx
  521.                inx
  522.                inx
  523.                cpx #8
  524.                bne MovOff
  525.  
  526.                lda WxOffSet             ;adjust offsets
  527.                clc
  528.                adc #20
  529.                sta WxOffset
  530.                lda WyOffset
  531.                clc
  532.                adc #12
  533.                cmp #120                 ;if we get too low, start at top
  534.                bne DoYset
  535.                lda #12
  536. doYset         sta WyOffset
  537.  
  538.  
  539.  
  540. **********
  541. *
  542. * Now, Finally, create the new window
  543. *
  544. **********
  545.  
  546. Finally        PushLong #0              ; space for result
  547.                pushlong #WindowParamBlock
  548.                _NewWindow
  549.  
  550.                pla
  551.                sta whichwindow
  552.                pla
  553.                sta whichwindow+2
  554.  
  555.                PushLong WhichWindow     ; set port to new top window
  556.                _SetPort
  557.  
  558.                lda PicHandle            ; unlock handle
  559.                ldx PicHandle+2
  560.                jsr Unlock
  561.  
  562.  
  563. **********
  564. *
  565. * Force origin boundaries (see Manual definition of Window Mgr's SetOriginMask)
  566. *
  567. **********
  568.  
  569.                PushWord #$FFFE
  570.                PushLong  whichwindow
  571.                _SetOriginMask
  572.  
  573.                _InitCursor
  574.  
  575.                clc                      ; carry clear return: we opened it
  576.                rts
  577.                end
  578.  
  579. ****************************************************************
  580. *
  581. * WindowData
  582. *
  583. ****************************************************************
  584. WindowData     data
  585. WindowParamBlock anop
  586.                dc i2'WindowEnd-WindowParamBlock'
  587.                dc i2'FTitle+FClose+FRScroll+FBScroll+FGrow+FZoom+FMove+FVis'
  588. windaddr       dc i4'0'                 Ptr to title
  589. refcon         dc i4'0'                 RefCon
  590.                dc i2'0,0,0,0'           Full Size (0= default)
  591.                dc i4'0'                 Color Table Pointer
  592.                dc i2'0'                 Vertical origin
  593.                dc i2'0'                 Horizontal origin
  594. DataHeight     dc i2'200'               Data Area Height
  595. DataWidth      dc i2'640'               Data Area Width
  596.                dc i2'200'               Max Cont Height
  597. McW            dc i2'640'               Max Cont Width
  598.                dc i2'4'                 Number of pixels to scroll vertically.
  599.                dc i2'16'                Number of pixels to scroll horizontally.
  600.                dc i2'40'                Number of pixels to page vertically.
  601.                dc i2'160'               Number of pixels to page horizontally.
  602.                dc i4'0'                 Infomation bar text string.
  603.                dc i2'0'                 Info bar height     
  604.                dc i4'0'                 DefProc.
  605.                dc i4'0'                 Routine to draw info. bar.
  606. DrawRtn        dc i4'Paint'             Routine to draw content.
  607. SizPos         dc i2'0,0,0,0'           Size/pos of content 
  608.                dc i4'$FFFFFFFF'         Plane to put window up in.
  609.                dc i4'0'                 Address for window record (0 to allocate)
  610. WindowEnd      anop
  611.  
  612. Refptr         ds    4                     ;refcon pointer to 20 bytes
  613. ISizPos        dc    i'20,10,80,350'       ;Size/pos of content
  614.  
  615.  
  616. FiRecord       anop
  617. Ascent         ds 2
  618. Descent        ds 2
  619. Leading        ds 2
  620. WidMax         ds 2
  621.  
  622.                END
  623.  
  624. ****************************************************************
  625. *
  626. * OpenFilter
  627. *
  628. *   This routine is passed to SFGetFile to filter out the filetypes
  629. *   that are loadable by us.
  630. *
  631. * On entry, the stack looks like this:
  632. *
  633. *        |       previous contents      |
  634. *        |------------------------------|
  635. *        |       space for result       |  word
  636. *        |------------------------------|
  637. *        |  pointer to directory entry  |  long
  638. *        |------------------------------|
  639. *        |        return address        |  3 bytes
  640. *        |------------------------------|
  641. *        |                              |  <- SP
  642. *
  643. ****************************************************************
  644.  
  645. OpenFilter     start
  646.                using GlobalData
  647.  
  648.                phb                      ; save DBR (and even out RTL addr)
  649.                phk                      ; set DBR back to this bank
  650.                plb
  651.                pla                      ; save the return address
  652.                sta RtnAddr
  653.                pla
  654.                sta RtnAddr+2
  655.  
  656.                tdc                      ; save the ROM's ZP
  657.                sta DPSave
  658.                lda MyZP                 ; and swap in ours
  659.                tcd
  660.  
  661.                pla                      ; now get the pointer to the
  662.                sta 0                    ; directory entry
  663.                pla
  664.                sta 2
  665.  
  666.                ldx #1                   ; assume visible and dimmed
  667.  
  668.                ldy #$10                 ; look at the filetype byte
  669.                lda [0],y
  670.                and #$00FF               ; don't look at the entire word
  671.  
  672.                cmp #$C1
  673.                bne NotPicFile           ; pass on all others
  674.                ldx #2                   ; show it as a selectable entry
  675.  
  676. NotPicFile     txa
  677.                sta 1,s                  ; pass it back on the stack
  678.  
  679.                lda DPSave               ; point back to the old DP
  680.                tcd
  681.  
  682.                lda RtnAddr+2            ; and put the return address back
  683.                pha
  684.                lda RtnAddr
  685.                pha
  686.                plb                      ; restore old DBR
  687.  
  688.                rtl
  689.  
  690. DPSave         ds 2
  691. RtnAddr        ds 4
  692.  
  693.                end
  694.  
  695. ****************************************************************
  696. *
  697. * FindMaxWidth - this routine finds out how wide the window
  698. *        should be for the currently installed font.
  699. *
  700. ****************************************************************
  701.  
  702. FindMaxWidth   start
  703.                using WindowData
  704.                using FontData
  705.                using GlobalData
  706.  
  707.                PushWord #0              ; save prev set mono/pro flag
  708.                _GetFontFlags
  709.  
  710.                ldy #oFlag               ; keep the result on the stack while
  711.                lda [0],y                ; we set it to what we want (as
  712.                lsr a                    ; defined by its window type set up
  713.                and #$0001               ; when we open this window)
  714.                pha
  715.                _SetFontFlags
  716.       
  717.                stz MaxSoFar
  718.                lda #1
  719.                sta LineCounter
  720. LineLoop       anop
  721.  
  722.                PushWord #0              ; space for width result.
  723.                phk                      ; Get a pointer to the current line.
  724.                phk                      ; The upper word is the same as the
  725.                pla                      ; program bank.
  726.                and #$00FF
  727.                pha
  728.                lda LineCounter          ; The lower word is stored in a table.
  729.                asl a
  730.                tax
  731.                lda LineTable,x
  732.                pha
  733.                _StringWidth
  734.  
  735.                pla                      ; How does this line compare with the
  736.                cmp MaxSoFar             ; previous longest line?
  737.                bcc LessThan
  738.                sta MaxSoFar             ; > or =, so save it as record holder.
  739.    
  740. LessThan       anop
  741.                inc LineCounter          ; bump current line 
  742.                lda LineCounter
  743.                cmp #NumLines
  744.                bcc LineLoop
  745.  
  746.                lda MaxSoFar             ; Get the width of longest line.
  747.                clc                      ; Add in room for left and right margins
  748.                adc #10
  749.                sta DataWidth
  750.  
  751.                _SetFontFlags            ; restore old settings
  752.  
  753.                rts
  754. LineCounter    ds 2
  755. MaxSoFar       ds 2
  756.                end
  757.  
  758. ****************************************************************
  759. *
  760. * DoCloseItem
  761. *
  762. *   Close a window, and dispose of extra data (in WrefCom)
  763. *   and remove it from window list.  If no windows, then dim "Window"
  764. *   menu and disallow printing.
  765. *
  766. ****************************************************************
  767. DoCloseItem    START
  768.                using GlobalDATA
  769.  
  770.                pushlong #0              ;it's the front window we're adding in
  771.                _FrontWindow
  772.                pla
  773.                sta whichwindow
  774.                pla
  775.                sta whichwindow+2        ; get result for pushing in a sec.
  776.                ora WhichWindow          ; was there one?
  777.                bne ThereIsOne
  778. GotIt          rts                      ; quit now
  779.  
  780. ThereIsOne     PushLong WhichWindow     ; if it is a system window, this will
  781.                _CloseNDAByWinPtr        ; close it
  782.                bcc GotIt                ; no error so done
  783.  
  784. ; Must be one of mine.
  785. dothecls       PUSHLONG #0              ;space for result
  786.                PUSHLONG whichwindow
  787.                _GetWrefCon              ;refcon has handle to data
  788.  
  789.                pla
  790.                sta temp2Handle
  791.                plx
  792.                stx Temp2Handle          ;the refcon to de-allocate
  793.  
  794.                jsr deref
  795.                sta 0
  796.                stx 2
  797.  
  798.                ldy #oHandle
  799.                lda [0],y
  800.                sta PicHandle
  801.                iny
  802.                iny
  803.                lda [0],y
  804.                sta PicHandle+2          ; the pichandle (we'll de-allocate)
  805.  
  806.                ldy #oFlag               ; check if picture or font
  807.                lda [0],y
  808.                beq itsapic
  809.                stz PicHandle            ; flag so we don't dispose
  810.                stz PicHandle+2
  811.  
  812.  
  813. ItsAPic        jsr AdjWind              ; goes and pulls window from WindowList
  814.  
  815.                clc                      ; position returned in a-reg.
  816.                adc #300                 ; start at 300
  817.                sta IDdelete             ; the MenuID to del-allocate
  818.  
  819.                lda windex               ; if only one, we must be special
  820.                cmp #1
  821.                bne MoreThanOne
  822.  
  823.                pushlong #origitem       ; We're now deleting the only window
  824.                pushword #0              ; left.
  825.                pushword #WindowsMenuID
  826.                _InsertMItem             ; add old "no windows" menu item.
  827.  
  828.                Pushword #$0080          ;Disable windows menu
  829.                PushWord #WindowsMenuID
  830.                _SetMenuFlag
  831.  
  832.                Lda #True
  833.                Sta NeedToUpdate
  834.  
  835.                stz PrintAvail           ; Disallow printing
  836.  
  837.                lda #20                  ; reset start loc for window sizing
  838.                sta WxOffset
  839.                lda #12
  840.                sta WyOffSet
  841.  
  842.  
  843. MoreThanOne    lda IDdelete
  844.                pha                      ;now delete this item from menus
  845.                _DeleteMItem
  846.                dec windex
  847.  
  848.                lda windex               ;now, renumber list
  849.                beq nomore               ; none left, skip
  850.  
  851.                sta IdCounter            ; counts how many
  852.                lda #300                 ; always the starting no.
  853.                sta IDstart              ; will be first
  854.                sta IDNew                ; and the new one
  855. back           lda IdStart
  856.                cmp IdDelete             ; is it the one we deleted?
  857.                bne DoIt                 ; nope, go re-set ID
  858.                inc Idstart              ; yes, skip over it
  859.                bra back
  860. DoIt           pushword IdNew
  861.                pushword IdStart
  862.                _SetMItemId               ; reset
  863.                inc IdStart
  864.                inc IdNew
  865.                dec IdCounter
  866.                bne back
  867.  
  868. NoMore         lda #0                   ; re-calc size
  869.                pha
  870.                pha
  871.                PushWord #WindowsMenuID
  872.                _CalcMenuSize
  873.  
  874.                Pushlong Temp2Handle     ;get rid of refcon area
  875.                _DisposeHandle
  876.  
  877.                lda PicHandle            ;is it font
  878.                bne dodisp
  879.                lda PicHandle+2
  880.                beq skipdisp
  881.  
  882.  
  883. DoDisp         Pushlong PicHandle       ;get rid of picture area
  884.                _DisposeHandle
  885.  
  886. SkipDisp       PushLong WhichWindow     ;get rid of window
  887.                _CloseWindow
  888.  
  889. skip           rts
  890. ****************************
  891. *
  892. * AdjWind finds and deletes a window list item which matches
  893. * "WhichWindow" and returns in a-reg. where it's position was
  894. *
  895. * Note: it's optimized to find things near end of list
  896. *       (if you'd prefer the other end, you'd need some different logic,
  897. *        but here, generally, you'll open, look at it, and close it, so
  898. *        this method seems best)
  899. *
  900. ****************************
  901. AdjWind        lda Windex
  902.                tay                      ;use this to count thru
  903.                dec a
  904.                asl a                    ; pt. before last for end (a-2)*4
  905.                asl a
  906.                sta IDCounter
  907. adjloop        dey
  908.                bmi AdjDone
  909.                tya
  910.                asl a
  911.                asl a
  912.                tax
  913.                lda WindowList,x         ;get the pointer (uniqueness exists)
  914.                cmp WhichWindow
  915.                bne adjloop
  916.                lda WindowList+2,x
  917.                cmp WhichWindow+2
  918.                beq shoveChk
  919.                bra Adjloop
  920.  
  921. ShoveIt        lda WindowList+4,x       ;now shove things up
  922.                sta WindowList,x
  923.                lda WindowList+6,x
  924.                sta WindowList+2,x
  925.                inx
  926.                inx
  927.                inx
  928.                inx
  929. ShoveChk       cpx IdCounter                                
  930.                bne shoveit
  931. AdjDone        tya
  932.                rts
  933.  
  934. IdNew          ds 2
  935. IdStart        ds 2
  936. IdCounter      ds 2
  937. IDdelete       ds 2
  938.  
  939.                END
  940.  
  941. ****************************************************************
  942. *
  943. * Paint
  944. *
  945. * This draws picture in the window when task master calls.
  946. *
  947. ****************************************************************
  948. Paint          START
  949.                using GlobalData
  950. **********
  951. *
  952. *  get my own zero page
  953. *
  954. **********
  955.  
  956.  
  957.                phb
  958.                phk
  959.                plb
  960.  
  961.                phd
  962.                lda MyZP
  963.                tcd
  964.  
  965. **********
  966. *
  967. * get the correct window port (got here from within taskmaster)
  968. *
  969. **********
  970.  
  971.                pushlong #0
  972.                _GetPort
  973.                plx
  974.                ply                      ;get result for pushing in a sec.
  975.  
  976.                PUSHLONG #0              ;space for result
  977.                phy
  978.                phx                      ; saved the port here
  979.                _GetWrefCon              ;refcon has handle to data
  980.  
  981.                pla
  982.                sta Temphandle
  983.                plx
  984.                stx TempHandle+2
  985.  
  986.                jsr Deref                ; de reference
  987.                sta 0
  988.                stx 2
  989.  
  990.                ldy #oHandle             ; get handle to pic data
  991.                lda [0],y
  992.                sta picptr
  993.                pha
  994.                iny
  995.                iny
  996.                lda [0],y
  997.                sta picptr+2
  998.                tax
  999.                pla
  1000.  
  1001.                jsl PaintIt
  1002.  
  1003.                lda TempHandle
  1004.                ldx TempHandle+2
  1005.                jsr Unlock
  1006.  
  1007.                pld
  1008.                plb
  1009.  
  1010.                rtl
  1011.  
  1012.                END
  1013.  
  1014. ****************************************************************
  1015. *
  1016. * PaintIt
  1017. *
  1018. * The routine which actually does the painting when passed the
  1019. * the handle to the picture in the a & x registers.
  1020. *
  1021. ****************************************************************
  1022. PaintIt        START
  1023.                using GlobalData
  1024.  
  1025.                phx                      ; save this on stack
  1026.                pha
  1027.  
  1028.                jsr deref                ;deref. picture handle
  1029.                sta picptr
  1030.                stx picptr+2
  1031.  
  1032.                PushLong #SrcLocInfo
  1033.                PushLong #SrcRect
  1034. Pshmor         PushWord #0              ; x
  1035.                PushWord #0              ; y
  1036.                PushWord #0              ; copy
  1037.                _PPToPort
  1038.  
  1039.                pla
  1040.                plx
  1041.                jsr Unlock
  1042.  
  1043.                rtl
  1044.  
  1045.                END
  1046.  
  1047. ****************************************************************
  1048. *
  1049. * DoGoAway -- not necessary because we handle it the same as
  1050. *             DoCloseItem.
  1051. *
  1052. ****************************************************************
  1053.  
  1054.  
  1055.  
  1056. ****************************************************************
  1057. *
  1058. * DoWindow
  1059. *
  1060. * Selects and shows window in response to menu selection.
  1061. *
  1062. ****************************************************************
  1063. DoWindow       START
  1064.                using GlobalDATA
  1065.  
  1066.                PUSHLONG WHICHWINDOW     ; select first so it only redraws
  1067.                _SelectWindow            ; once
  1068.  
  1069.                PUSHLONG WHICHWINDOW
  1070.                _ShowWindow
  1071.  
  1072.                rts
  1073.  
  1074.                END
  1075.